package com.example.lanouhn.firstapp.activity.utils;

import com.alibaba.fastjson.JSON;
import com.example.lanouhn.firstapp.activity.model.Carousel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/29.
 */
public class CarouselJson {

    /**
     * 解析首页轮播图
     *
     * @param json
     * @return
     */
    public static List<Carousel> getCarousel(String json) {
        //创建数组来储存数据
        List<Carousel> iList = new ArrayList<>();

        try {
            JSONObject job1 = new JSONObject(json);
            //获取解析的数据
            JSONArray array = job1.getJSONArray("datas");
            for (int i = 0; i < array.length(); i++) {
                JSONObject jo = array.getJSONObject(i);
                String adv_list = jo.getString("adv_list");
                JSONObject job2 = new JSONObject(adv_list);
                //解析数据
                JSONArray array1 = job2.getJSONArray("item");


                //使用fastJson解析
                iList = new ArrayList<>(JSON.parseArray(array1.toString(), Carousel.class));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return iList;

    }


}
